package cn.com.cfae.iras.doc.analyze.components.rationality;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.FormatTimeUtil;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 内容合理性
 * M-3-1
 * 1、从M-3-1获取债权债务登记日、上市流通日，检查上市流通日=债权债务登记日+1是否相等，如果不一致，提示人工核对；
 * 发行阶段
 */
@Component("R1015")
@Scope("prototype")
public class R1015 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(R1015.class);
    @Autowired
    private HandingData handingData;
    @Autowired
    private FormatTimeUtil formatTime;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取R1015组件M");
        List<ComponentResult> componentResults = checkRulesR1015(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;

    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取R1015组件BM");
        List<ComponentResult> componentResults = checkRulesR1015(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取R1015组件DM");
        List<ComponentResult> componentResults = checkRulesR1015(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取R1015组件BDM");
        List<ComponentResult> componentResults = checkRulesR1015(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }


    private List<ComponentResult> checkRulesR1015(String model,String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int pageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList = null;
        boolean flag = false;
        int startPage = 0;
        boolean isSSLTRNull = true;
        boolean isDJRNull = true;
        String ssltrText = null;
        String djrText = null;

        int zqzwPageNum = 0;
        List<ComponentResult.TextPosition> zqzwContentAndPositionList = null;

        boolean isAllErr = false;
        boolean isAllSeem = false;
        try {
            if (extractData!=null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPage = chapterModel.getStartPage();

                //上市流通日
                extractItem = getExtractItem(extractData,model,"FXTK","SSLTR");
                if (extractItem!=null){
                    KVText value = extractItem.getValue();
                    KVText key = extractItem.getKey();
                    ssltrText = value!=null?value.getText():null;
                    pageNum = value!=null?value.getPageNumber():startPage;

                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                }
                pageNum = pageNum!=0?pageNum:startPage;
                //债权债务登记日
                extractItem = getExtractItem(extractData,model,"FXTK","ZQZWDJR");
                if (extractItem != null){
                    KVText value = extractItem.getValue();
                    KVText zqzwkey = extractItem.getKey();
                    djrText = value!=null?value.getText():null;
                    zqzwPageNum = value!=null?value.getPageNumber():startPage;
                    zqzwContentAndPositionList = zqzwkey!=null?getContentAndPositionList(zqzwkey):null;
                }
                zqzwPageNum = zqzwPageNum!=0?zqzwPageNum:startPage;
            }
            if (StringUtils.isNotEmpty(ssltrText)){
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"上市流通日与债权债务登记日检查-上市流通日:"+ssltrText,"已抽取",false,getRegulationId(), pageNum,contentAndPositionList,"01");
                componentResults.add(componentResult);
            }else {
                isAllSeem = true;
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"上市流通日与债权债务登记日检查-上市流通日","未识别,建议人工核对",true,getRegulationId(), pageNum,contentAndPositionList,"03");
                componentResults.add(componentResult);
            }
            if (StringUtils.isNotEmpty(djrText)){
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"上市流通日与债权债务登记日检查-债权债务登记日:"+djrText,"已抽取",false,getRegulationId(), zqzwPageNum,zqzwContentAndPositionList,"01");
                componentResults.add(componentResult);
            }else {
                isAllSeem = true;
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"上市流通日与债权债务登记日检查-债权债务登记日","未识别,建议人工核对",true,getRegulationId(), zqzwPageNum,zqzwContentAndPositionList,"03");
                componentResults.add(componentResult);
            }
            if (StringUtils.isNotEmpty(ssltrText)&&StringUtils.isNotEmpty(djrText)){

                String ssltStr = formatTime.formatTime(ssltrText);
                String djrStr = formatTime.formatTime(djrText);
                if (djrStr.length()==8){
                    djrStr = formatTime.getTimeOfSet(djrStr,"DAY",1,"yyyyMMdd");
                    if (ssltStr.equals(djrStr)){
                        flag = true;
                    }
                }

                if (flag){
                    componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"上市流通日与债权债务登记日检查","合理",false,getRegulationId(), pageNum,contentAndPositionList,"01");
                    componentResults.add(componentResult);
                }else{
                    isAllErr = true;
                    componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"上市流通日与债权债务登记日检查","请人工核对",true,getRegulationId(), pageNum,contentAndPositionList,"02");
                    componentResults.add(componentResult);
                }
            }else {
                isAllSeem =true;
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"上市流通日与债权债务登记日检查","未识别,建议人工核对",true,getRegulationId(), startPage,contentAndPositionList,"03");
                componentResults.add(componentResult);
            }

            if (isAllErr){
                for (ComponentResult result: componentResults){
                    result.setExist("02");
                }
            }
            if (isAllSeem){
                for (ComponentResult result:componentResults){
                    result.setExist("03");
                }
            }

        }catch (Exception e){
            componentResults.clear();
            componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"上市流通日与债权债务登记日检查","未识别,建议人工核对",true,getRegulationId(), startPage,contentAndPositionList,"03");
            componentResults.add(componentResult);
            log.error(e.getMessage(),e);
        }

        return componentResults;
    }

    private ExtractItem getExtractItem(DocumentModel extractData, String chapter, String model, String key){
        ExtractItem extractItem  = null;
        if (extractData!=null){
            ChapterModel chapterModel = extractData.getChapterModel(chapter);
            if (chapterModel != null){
                ExtractBizModel extractMode = chapterModel.getModel(model);
                if (extractMode!=null){
                    extractItem = extractMode.getExtractItem(key);
                }
            }
        }
        return extractItem;
    }
}
